//
//  TodayViewController.m
//  XWZ_News_Work
//
//  Created by dllo on 16/1/19.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "TodayViewController.h"
#import "NewOneTableViewCell.h"
#import "NewTwoTableViewCell.h"
#import "NewThreeTableViewCell.h"
#import "NewsModel.h"
#import "NewsDeatilWebViewController.h"
#import "photoesViewController.h"

@interface TodayViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableV;
@property (nonatomic, strong) NSMutableArray *arr;

@end

@implementation TodayViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self createView];
    [self getData];

}


- (void)createView
{
    [self createTableView];
    [self createHeadView];
}

- (void)createTableView
{
    self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height - 70) style:UITableViewStylePlain];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    [self.view addSubview:self.tableV];
}

- (void)createHeadView
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:view.frame];
    imageV.image = [UIImage imageNamed:@"localnewsheader_background"];
    [view addSubview:imageV];
    
    UIImageView *imageH = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 70, 70)];
    imageH.backgroundColor = [UIColor redColor];
    imageH.center = CGPointMake(view.frame.size.width / 2, view.frame.size.height / 2);
    imageH.image = [UIImage imageNamed:@"iconfont-24xiaoshi-1"];

    [view addSubview:imageH];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, imageH.frame.origin.y + imageH.frame.size.height + 10, view.frame.size.width, 10)];
    label.text = @"聚焦今日时事, 浓缩新闻精华";
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    [view addSubview:label];
    
    self.tableV.tableHeaderView = view;
    
    
}

- (void)getData
{
    
    NSString *urlStr = @"http://c.m.163.com/nc/article/list/T1429173683626/0-20.html";
    [xwzAFNetTool getNetWithURL:urlStr body:nil headFile:nil responStyle:XWZ_JSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        self.arr = [NSMutableArray array];
        NSMutableArray *array = [responseObject objectForKey:@"T1429173683626"];
        for (NSMutableDictionary *tempDic in array) {
            NewsModel *model = [[NewsModel alloc] initWithDic:tempDic];
            [self.arr addObject:model];
        }
        
        
        [self.tableV reloadData];
//        NSLog(@"%@", [[self.arr objectAtIndex:0] title]);
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (0 == indexPath.row) {
        return 250 * HEIGHT;
    } else if (nil != [[self.arr objectAtIndex:indexPath.row] imgextra]) {
        return 130 * HEIGHT;
    } else {
    
    return 100;
    }

    
    
    
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsModel *model = [self.arr objectAtIndex:indexPath.row];
    if (0 == indexPath.row) {
        static NSString *cellBig = @"cellB";
        NewThreeTableViewCell *cellB = [tableView dequeueReusableCellWithIdentifier:cellBig];
        if (nil == cellB) {
            cellB = [[NewThreeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellBig];
            
        }
        [cellB.image sd_setImageWithURLStr:model.imgsrc placeholderImage:[UIImage imageNamed:@"lol-tupian"]];
        cellB.labelTitle.text = model.title;
        cellB.labelSubject.text = model.digest;
        return cellB;

    } else if (nil != model.imgextra) {
        static NSString *cellPhoto = @"cellP";
        NewTwoTableViewCell *cellP = [tableView dequeueReusableCellWithIdentifier:cellPhoto];
        if (nil == cellP) {
            cellP = [[NewTwoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellPhoto];
        }
        [cellP.imageL sd_setImageWithURLStr:model.imgsrc placeholderImage:[UIImage imageNamed:@"lol-tupian"]];
        [cellP.imageC sd_setImageWithURLStr:[[model.imgextra objectAtIndex:0] objectForKey:@"imgsrc"] placeholderImage:[UIImage imageNamed:@"lol-tupian"]];
        [cellP.imageR sd_setImageWithURLStr:[[model.imgextra objectAtIndex:1] objectForKey:@"imgsrc"] placeholderImage:[UIImage imageNamed:@"lol-tupian"]];
        return cellP;
    } else {
        static NSString *cellStr = @"cell";
        NewOneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
        if (nil == cell) {
            cell = [[NewOneTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellStr];
        }
        [cell.image sd_setImageWithURLStr:model.imgsrc placeholderImage:[UIImage imageNamed:@"lol-tupian"]];
        cell.labelSubject.text = model.digest;
        cell.labelTitle.text = model.title;
        return cell;
    }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    NewsModel *model = [self.arr objectAtIndex:indexPath.row];
    NewsDeatilWebViewController *deatilWebVC = [[NewsDeatilWebViewController alloc] init];
    
    if (![model.skipType isEqualToString:@"photoset"]) {
        deatilWebVC.docId = model.docid;
        [self.navigationController pushViewController:deatilWebVC animated:YES];
    } else {
        photoesViewController *photoVC = [[photoesViewController alloc] init];
        photoVC.photosetID = model.photosetID;
        [self.navigationController pushViewController:photoVC animated:YES];
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
